<?php

/*
 * This file will perform the ajax for adding the document logs in a location.
 * 
 * Created by: Peter Agno Jr.
 * Date created: Octber 23, 2011
 * 
 * In parameters: documentId, position
 * Out parameters: document logs
 */

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {
    include('../includes/siteConfig.php'); 

    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');

    $documentId = $_POST['documentId'];
    $scrnPostn = $_POST['scrnPostn'];
    
    // Add the document log screenlet header
    $logInfo = "<span class='screenlet'>Logs</span>";
    
    //********************************************************************************************************
    
    /*
     * Define the functions in here
     */

        // DISPLAY THE DEPARTMENTS AND POSITIONS OF USER PROPERLY
        function displayDeptPosResult($deptPosResult, $docFlag, $logInfo) {
            if ( ( mysql_num_rows($deptPosResult) - 1 ) > 0 ) {
                // DISPLAY ALL OF THE DEPARTMENTS AND POSITIONS OF USER IF IT HAS MORE THAN ONE

                // DISPLAY DEPARTMENT        
                // IF ELSE BELOW WAS COMMENT OUT BECAUSE THE INDENTION FOR ATTACHMENTS IN THE LOGS WILL NOT BE USED
                // COMMENT OUT FOR ARCHIVING PURPOSE, DO NOT DELETE YET    
    //            if ( $docFlag == 'Attach' )
    //                echo "<td style='padding-left: 4.5%;'>";
    //            else
    //                echo "<td style='padding-left: 2.8%;'>";
                    $logInfo = $logInfo . "<td>";

    //            echo "<ol type='A'>";

                for( $index = 0 ; $index < mysql_num_rows($deptPosResult) ; $index++ ){
                    mysql_data_seek($deptPosResult, $index);
                    $deptPosRow = mysql_fetch_array($deptPosResult);
                    $logInfo = $logInfo . ($index+1) .". ".$deptPosRow['departmentName']."<br/>";
                }
    //            echo "</ol></td>";
                $logInfo = $logInfo . "</td>";

                // DISPLAY POSITION
                // IF ELSE BELOW WAS COMMENT OUT BECAUSE THE INDENTION FOR ATTACHMENTS IN THE LOGS WILL NOT BE USED
                // COMMENT OUT FOR ARCHIVING PURPOSE, DO NOT DELETE YET 
    //            if ( $docFlag == 'Attach' )
    //                echo "<td style='padding-left: 4.5%;'>";
    //            else
    //                echo "<td style='padding-left: 2.8%;'>";
                    $logInfo = $logInfo . "<td>";

    //            echo "<ol type='A'>";

                for( $index = 0 ; $index < mysql_num_rows($deptPosResult) ; $index++ ){
                    mysql_data_seek($deptPosResult, $index);
                    $deptPosRow = mysql_fetch_array($deptPosResult);
                    $logInfo = $logInfo . ($index+1) .". ".$deptPosRow['positionName']."<br/>";
                }
    //            echo "</ol></td>";
                $logInfo = $logInfo . "</td>";

            }
            else {
                // DISPLAY ALL OF THE DEPARTMENTS AND POSITIONS OF USER IF IT HAS ONLY ONE
                $deptPosRow = mysql_fetch_array($deptPosResult);
                // IF ELSE BELOW WAS COMMENT OUT BECAUSE THE INDENTION FOR ATTACHMENTS IN THE LOGS WILL NOT BE USED
                // COMMENT OUT FOR ARCHIVING PURPOSE, DO NOT DELETE YET 
    //            if ( $docFlag == 'Attach' ) {
    //                echo "<td style='padding-left: 2.2%;'>".$deptPosRow['departmentName']."</td>";
    //                echo "<td style='padding-left: 2.2%;'>".$deptPosRow['positionName']."</td>";
    //            }
    //            else {
                    $logInfo = $logInfo . "<td>".$deptPosRow['departmentName']."</td>";
                    $logInfo = $logInfo . "<td>".$deptPosRow['positionName']."</td>";
    //            }
            }
            
            return $logInfo;
        }

        // START - DISPLAY ALL DEPARTMENTS AND POSITIONS OF THE USER
        function displayDeptPos($userId, $stepId, $docFlag, $logInfo) {
            $query =
                "
                    -- Get departmentName and positionName
                    SELECT userId, stepId, DP.deptPosId as deptPosId, DP.departmentId as departmentId, DP.positionId as positionId, departmentName, positionName
                    FROM

                            -- Get the deptPosId of the user and also check if the user's deptPosId is connected with the step 
                            (SELECT userId, stepId, RDP.deptPosId as deptPosId
                            FROM
                                    (SELECT *
                                    FROM Account_Dept_Pos
                                    WHERE userId = '$userId') as ADP,
                                    (SELECT *
                                    FROM RouteStep_Dept_Pos
                                    WHERE stepId = '$stepId') as RDP

                            WHERE ADP.deptPosId = RDP.deptPosId) as UserStepInfo, Department_Position as DP, Department as D, Position as P

                    WHERE UserStepInfo.deptPosId = DP.deptPosId AND DP.departmentId = D.departmentId AND DP.positionId = P.positionId
                ";

            $deptPosResult = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

            // IF THERE WAS A CONNECTION BETWEEN THE USER AND STEP, IT MEANS THAT
            // THE POSITION OF THE USER, IS ONE OF THE SETUP POSITIONS FOR THAT STEP
            if ( mysql_num_rows($deptPosResult) != 0 ) {
                $logInfo = displayDeptPosResult($deptPosResult, $docFlag, $logInfo);
            }
            else {
                // ELSE, SEARCH FOR ALL OF THE USER'S POSITION
                $query =
                    "
                        -- Get departmentName and positionName
                        SELECT userId, DP.deptPosId as deptPosId, DP.departmentId as departmentId, DP.positionId as positionId, departmentName, positionName
                        FROM Account_Dept_Pos as ADP, Department_Position as DP, Department as D, Position as P
                        WHERE userId = '$userId' AND ADP.deptPosId = DP.deptPosId AND DP.departmentId = D.departmentId AND DP.positionId = P.positionId
                    ";

                $deptPosResult = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

                $logInfo = displayDeptPosResult($deptPosResult, $docFlag, $logInfo);
            }

            mysql_free_result($deptPosResult);
            
            return $logInfo;
        } // END - DISPLAY ALL DEPARTMENTS AND POSITIONS OF THE USER
        
        // Start - Check if the current step of the document if it is needed to be color red
        function rowIsRed($documentId, $logId) {
            // Check if the logId that was passed was the 2nd or 3rd from the logs (in desc)
            $query =
            "
                SELECT *
                FROM Log
                WHERE documentId = '$documentId'
                ORDER BY logId DESC
            ";
            
            $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
            
            $isSecond = false;
            $count = 0;
            while ( $row = mysql_fetch_array($result) ) {
                if ( $count <= 2 && $logId == $row['logId'] && ($row['activity'] == "Cancelled" || $row['activity'] == "Returned For Revisions" || $row['activity'] == "Rejected") ) {
                    $isSecond = true;
                }
                
                $count++;
            }
            
            return $isSecond;
        } // End - Check if the current step of the document if it is needed to be color red
    
    //******************************************************************************************************
    
    // CHECK FIRST IF THE $documentId is a main document or attachment
    $query =
        "
            SELECT *
            FROM MainDocument
            WHERE mainDocumentId = '$documentId'
        ";

    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    $isMainDoc = 0;
    if (mysql_num_rows($result) > 0) {
        $isMainDoc = 1;
    }
    
    switch ($isMainDoc) {
        case 1:
        // START - ##############PRINT MAIN DOCUMENT AND ATTACHMENT LOGS##############

            // DISPLAY TABLE HEADERS FOR MAIN DOCUMENT
            $logInfo = $logInfo . "<table class='logMainDocTable'>";

            // DISPLAY TRACK DOCUMENT
            $logInfo = $logInfo . "<tr id='logMainDocTrackRow'>";
            // This row will be used for the printing of track document
            //include('trackDocument.php'); *************************************************************************
            $logInfo = $logInfo . "</tr>";
            
            // Add log headers
            $logInfo = $logInfo .
                "
                    <tr>
                        <td class='logHeader'>Step</td>
                        <td class='logHeaderDoc'>Document</td>
                        <td class='logHeader'>Action</td>
                        <td class='logHeader'>User</td>
                        <td class='logHeader'>Department</td>
                        <td class='logHeader'>Position</td>
                        <td class='logHeader'>Date</td>
                        <td class='logHeader'>Time</td>
                        <td class='logHeader'><img src='images/log/attach_icon.png' alt='Attachment' width='24' height='24' /></td>
                    </tr>
                ";
 
            // START - ##############DISPLAY LOGS##############
            // QUERY THE LOGS $documentId
            $query =
                "
                    -- Get the LogMessage if any
                    SELECT NoLogMsg.logId as logId, message, documentId, userId, stepId, activity, mainDocumentId, subRouteId, stepNumber, documentSetupId, documentName, firstName, middleName, lastName, exAccId, exAccName, logDateTime, DATE_FORMAT(logDateTime, '%m/%d/%Y') as logDate, TIME_FORMAT(logDateTime, '%r') as logTime
                    FROM

                        -- Get the full name of the userId
                        -- Get also the external account name if it has no first, middle, and/or last name
                        (
                            -- Left join external accounts
                            SELECT logId, documentId, NoExAccName.userId as userId, stepId, activity, mainDocumentId, subRouteId, stepNumber, documentSetupId, documentName, firstName, middleName, lastName, EA.exAccId as exAccId, EA.exAccName as exAccName, logDateTime
                            FROM

                                (SELECT logId, documentId, LogNoUserFullName.userId as userId, stepId, activity, mainDocumentId, subRouteId, stepNumber, documentSetupId, documentName, firstName, middleName, lastName, logDateTime 
                                FROM

                                        -- Get the documentSetupId and documentName
                                        (SELECT logId, LogWithSubRoute.documentId as documentId, userId, stepId, activity, mainDocumentId, subRouteId, stepNumber, documentSetupId, documentName, logDateTime
                                        FROM

                                                -- Identify the logs with subroute column which will be used for the show and hide button
                                                (SELECT logId, documentId, userId, LogNoSubRoute.stepId as stepId, activity, mainDocumentId, subRouteId, stepNumber, logDateTime 
                                                FROM 

                                                        -- Identify the logs of the main document from the passed parameter
                                                        (SELECT *
                                                        FROM

                                                                -- Identify main documents from attachments
                                                                (SELECT Log.logId as logId, Log.documentId as documentId, Log.userId as userId, Log.stepId as stepId, Log.activity as activity, Attachment.mainDocumentId as mainDocumentId, logDateTime  
                                                                FROM Log LEFT JOIN Attachment
                                                                ON Log.documentId = Attachment.attachmentId) as TheLogs

                                                        WHERE TheLogs.documentId = '$documentId' OR TheLogs.mainDocumentId = '$documentId') as LogNoSubRoute

                                                LEFT JOIN RouteStep
                                                ON LogNoSubRoute.stepId = RouteStep.stepId) as LogWithSubRoute,

                                                -- Get documentSetupId and documentName
                                                (SELECT MainDocAttachUnion.documentSetupId as documentSetupId, documentName, documentId
                                                FROM

                                                        -- Union MainDocument and Attachment
                                                        ((SELECT mainDocumentId as documentId, documentSetupId
                                                        FROM MainDocument)
                                                        UNION
                                                        (SELECT attachmentId as documentId, documentSetupId
                                                        FROM Attachment)) as MainDocAttachUnion, DocumentSetup

                                                WHERE MainDocAttachUnion.documentSetupId = DocumentSetup.documentSetupId) as DocSetupIdAndName

                                        WHERE LogWithSubRoute.documentId = DocSetupIdAndName.documentId
                                        ORDER BY logId) as LogNoUserFullName

                                LEFT JOIN Account
                                ON LogNoUserFullName.userId = Account.userId

                                ORDER BY logDateTime) as NoExAccName

                            LEFT JOIN ExternalAccount as EA
                            ON NoExAccName.userId = EA.exAccName
                        ) as NoLogMsg

                    LEFT JOIN LogMessage
                    ON NoLogMsg.logId = LogMessage.logId
                ";

            $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

            // INITIALIZE THE NEEDED VARIABLES
            $mainDocIdxPtr =  mysql_num_rows($result) - 1;
            $attachIdxPtr =  mysql_num_rows($result);
            $row = "";
            $tempActivity = "";
            $tempSubRouteId = "";
            $attachmentRowClass = "";
//            $totalStepCount = getTotalStepNum($documentId);
            $totalStepCount = 0;
            $showViewAllLogs = false;

            // START - DISPLAYING LOGS OF MAIN DOCUMENT AND ATTACHMENTS
            for( $expandCounter = 0 ; $mainDocIdxPtr >= 0 ; $mainDocIdxPtr--, $expandCounter++ ){
                mysql_data_seek($result, $mainDocIdxPtr);
                $row = mysql_fetch_array($result);

                // IF $tempRow['mainDocumentId'] IS NULL, OR IS CLASSIFIED AS MAIN DOCUMENT
                if ( $row['mainDocumentId'] == NULL ) {
                    // DISPLAY MAIN DOCUMENT INFO
                    
                    // Prepare row color
                    $logInfo = $logInfo . "<tr id='mainDocLogRow' ";
                    
                    // Check if this row should be color red
                    if ( rowIsRed($documentId, $row['logId']) == true && ($row['activity'] == "Cancelled" || $row['activity'] == "Returned For Revisions" || $row['activity'] == "Rejected") ) {
                        $logInfo = $logInfo . "class='mainDocLogRedRow mainDocLogRow ";
                    }
                    else {
                        $logInfo = $logInfo . "class='mainDocLogGreenRow mainDocLogRow ";
                    }
                    
                    // Hide the other logs if there are 5 rows
                    if ( $totalStepCount > 4 ) {
                        // Add extra class for the show and hide of main document
                        $logInfo = $logInfo . "viewAllLogsRow' style='display:none;' ";
                        $showViewAllLogs = true;
                    }
                    else {
                        $logInfo = $logInfo . "' ";
                    }
                    $totalStepCount++;
                    
                    $logInfo = $logInfo . ">";
                    
                    $logInfo = $logInfo . "<td>".$row['stepNumber']."</td>";
                    $logInfo = $logInfo . "<td>".$row['documentName']."</td>";

                    // Add information icon, if any
                    $logInfo = $logInfo . "<td>".$row['activity']." ";
                    if ( $row['message'] != NULL ) {
                        $logInfo = $logInfo . "<img class='infoIcon' id='info_".$row['logId']."_".$scrnPostn."' src='images/log/info_blu.png' alt='Info Icon' width='12' height='12' /> ";
                    }
                    $logInfo = $logInfo . "</td>";

                    // GET THE ACTIVITY THAT WILL BE USED IN THE TR OF CERTAIN ATTACHMENTS
                    $tempActivity = explode(' ', $row['activity']);

                    // START - PRINT THE FULL NAME OF THE ACCOUNTED PERSON PROPERLY
                    $logInfo = $logInfo . "<td>";

                        if ( $row['exAccId'] == NULL ) {
                            if( $row['firstName'] != NULL ) {
                                $logInfo = $logInfo . $row['firstName']." ";
                            }

                            // Middle name is removed due to insufficient space
        //                        if( $row['middleName'] != NULL ) {
        //                            echo $row['middleName']." ";
        //                        }

                            $logInfo = $logInfo . $row['lastName']." ";
                        }
                        else {
                            $logInfo = $logInfo . $row['exAccName']." ";
                        }

                    $logInfo = $logInfo . "</td>";
                    // END - PRINT THE FULL NAME OF THE ACCOUNTED PERSON PROPERLY

                    // START - DISPLAY ALL DEPARTMENTS AND POSITIONS OF THE USER
                    $logInfo = displayDeptPos($row['userId'], $row['stepId'], 'MainDoc', $logInfo);
                    // END - DISPLAY ALL DEPARTMENTS AND POSITIONS OF THE USER

                    $logInfo = $logInfo . "<td>".$row['logDate']."</td>";
                    $logInfo = $logInfo . "<td>".$row['logTime']."</td>";

                    // GET THE SUB ROUTE ID THAT WILL BE USED IN THE TR OF CERTAIN ATTACHMENTS
                    if( $row['subRouteId'] == NULL ) {
                        $tempSubRouteId = "NONE";
                    }
                    else {
                        $tempSubRouteId = $row['subRouteId'];
                    }

                    $attachmentRowClass = $tempSubRouteId . "_" . $tempActivity[0] . "_" . $row['stepId'] . "_" . $expandCounter;

                    // DONE DISPLAYI9NG MAIN DOCUMENT INFO, DISPLAY NOW THE ATTACHMENTS

                    $attachIdxPtr--;
                    $shwHidBtnFlg = 0; // SHOW HIDE BUTTON FLAG

                    // START - DISPLAY ATTACHMENT INFO
                    while( $attachIdxPtr != $mainDocIdxPtr ) {

                        // THIS IF ELSE WILL WRITE THE BUTTON IF IT HAS ATTACHMENTS TO SHOW
                        // IF NO ATTACHMENTS TO SHOWN, THEN DO NOT WRITE THE BUTTON
                        if ( $shwHidBtnFlg == 0 ){
                            $logInfo = $logInfo . "<td><input class='siteGreenButton extendButton' id='".$attachmentRowClass."' type='button' value='View'/></td>";
                            $logInfo = $logInfo . "</tr>";
                            $shwHidBtnFlg = 1;
                        }

                        mysql_data_seek($result, $attachIdxPtr);
                        $row = mysql_fetch_array($result);

                        $logInfo = $logInfo . "<tr class='".$attachmentRowClass." attachLogGreenRow ";
                        
                        // Hide the other logs if there are 5 rows
                        if ( $totalStepCount > 4 ) {
                            // Add extra class for the show and hide of main document
                            $logInfo = $logInfo . "viewAllLogsAttchRow' style='display:none;'> ";
                            $showViewAllLogs = true;
                        }
                        else {
                            $logInfo = $logInfo . "' style='display:none;'> ";
                        }
                        
                        $logInfo = $logInfo . "<td class='noBorders'></td>";
                        $logInfo = $logInfo . "<td>".$row['documentName']."</td>";

                        // Add information icon, if any
                        $logInfo = $logInfo . "<td>".$row['activity']." ";
                        if ( $row['message'] != NULL ) {
                            $logInfo = $logInfo . "<img class='infoIcon' id='info_".$row['logId']."_".$scrnPostn."' src='images/log/info_blu.png' alt='Green Circle' width='12' height='12' /> ";
                        }
                        $logInfo = $logInfo . "</td>";

                        // START - PRINT THE FULL NAME OF THE ACCOUNTED PERSON PROPERLY
                        $logInfo = $logInfo . "<td>";

                            if ( $row['exAccId'] == NULL ) {
                                if( $row['firstName'] != NULL ) {
                                    $logInfo = $logInfo . $row['firstName']." ";
                                }

                                // Middle name is removed due to insufficient space
            //                        if( $row['middleName'] != NULL ) {
            //                            echo $row['middleName']." ";
            //                        }

                                $logInfo = $logInfo . $row['lastName']." ";
                            }
                            else {
                                $logInfo = $logInfo . $row['exAccName']." ";
                            }

                        $logInfo = $logInfo . "</td>";
                        // END - PRINT THE FULL NAME OF THE ACCOUNTED PERSON PROPERLY

                        // START - DISPLAY ALL DEPARTMENTS AND POSITIONS OF THE USER
                        $logInfo = displayDeptPos($row['userId'], $row['stepId'], 'Attach', $logInfo);
                        // END - DISPLAY ALL DEPARTMENTS AND POSITIONS OF THE USER

                        $logInfo = $logInfo . "<td>".$row['logDate']."</td>";
                        $logInfo = $logInfo . "<td>".$row['logTime']."</td>";
                        $logInfo = $logInfo . "<td class='noBorders'></td>";
                        $logInfo = $logInfo . "</tr>";

                        $attachIdxPtr--;
                    } // END - DISPLAY ATTACHMENT INFO

                    // IF NO ATTACHMENTS TO SHOWN, THEN DO NOT WRITE THE BUTTON
                    if ( $shwHidBtnFlg == 0 ) {
                        $logInfo = $logInfo . "<td> &nbsp </td>";
                        $logInfo = $logInfo . "</tr>";
                    }
                }
            // END - ##############DISPLAY LOGS##############
            } // END - DISPLAYING LOGS OF MAIN DOCUMENT AND ATTACHMENTS
            
            // Add extra row for the view of all logs
            if ( $showViewAllLogs == true ) {
                $logInfo = $logInfo . "<tr><td colspan='9'align='center'>";
                $logInfo = $logInfo . "<a href='javascript:void(0)' id='viewAllLogs'>View All</a>";
                $logInfo = $logInfo . "</td></tr>";
            }
        // END - ##############PRINT MAIN DOCUMENT AND ATTACHMENT LOGS##############
            break;

        default:
        // START - ##############PRINT ATTACHMENT LOGS##############
            // DISPLAY TABLE HEADERS FOR ATTACHMENT
            $logInfo = $logInfo .
                "
                    <table class='logAttachTable'>
                    <tr>                    
                        <td class='logHeader'>Step</td>
                        <td class='logHeader'>Document</td>
                        <td class='logHeader'>Action</td>
                        <td class='logHeader'>User</td>
                        <td class='logHeader'>Department</td>
                        <td class='logHeader'>Position</td>
                        <td class='logHeader logDate'>Date</td>
                        <td class='logHeader logTime'>Time</td>
                    </tr>
                ";
            
            // QUERY THE LOGS $documentId 
            $query = 
                "
                    -- Get the log message
                    SELECT NoLogMsg.logId, message, stepNumber, documentId, documentSetupId, documentName, stepId, activity, firstName, middleName, lastName, userId, exAccId, exAccName, logDateTime, DATE_FORMAT(logDateTime, '%m/%d/%Y') as logDate, TIME_FORMAT(logDateTime, '%r') as logTime
                    FROM

                        -- Get attachment name
                        (SELECT logId, stepNumber, documentId, NoAttachmentName.documentSetupId as documentSetupId, documentName, stepId, activity, firstName, middleName, lastName, userId, exAccId, exAccName, logDateTime
                        FROM

                            -- Get attachment type
                            (SELECT logId, stepNumber, documentId, documentSetupId, stepId, activity, firstName, middleName, lastName, userId, exAccId, exAccName, logDateTime
                            FROM

                                -- Get the stepNumber 
                                (SELECT logId, stepNumber, documentId, AttachLogsWithFullName.stepId, activity, firstName, middleName, lastName, userId, exAccId, exAccName, logDateTime
                                FROM

                                    -- Get the attachment logs with full name of the accounted persons
                                    -- Get also the external account name if it has no first, middle, and/or last name
                                    (
                                        -- Left join external accounts
                                        SELECT logId, documentId, stepId, activity, firstName, middleName, lastName, NoExAccName.userId as userId, EA.exAccId as exAccId, EA.exAccName as exAccName, logDateTime
                                        FROM
            
                                            (SELECT logId, documentId, stepId, activity, firstName, middleName, lastName, AttachLogs.userId as userId, logDateTime
                                            FROM

                                                -- Get the logs of the attachment
                                                (SELECT logId, documentId, stepId, activity, userId, logDateTime
                                                FROM Log
                                                WHERE documentId = '$documentId'
                                                ORDER BY logDateTime DESC) as AttachLogs

                                            LEFT JOIN Account
                                            ON AttachLogs.userId = Account.userId) as NoExAccName
            
                                        LEFT JOIN ExternalAccount as EA
                                        ON NoExAccName.userId = EA.exAccName
                                    ) as AttachLogsWithFullName

                                LEFT JOIN RouteStep
                                ON AttachLogsWithFullName.stepId = RouteStep.stepId) as NoAttachmentSetupId

                            LEFT JOIN Attachment
                            ON documentId = attachmentId) as NoAttachmentName

                        LEFT JOIN DocumentSetup
                        ON NoAttachmentName.documentSetupId = DocumentSetup.documentSetupId

                        ORDER BY LogDateTime DESC) as NoLogMsg

                    LEFT JOIN LogMessage
                    ON NoLogMsg.logId = LogMessage.logId
            
                    ORDER BY logId DESC
                ";
            
            $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
            
            $totalStepCount = 0; // This will be used for showing and hiding of attachment log rows
            
            while( $row = mysql_fetch_array($result) ) {
                // Hide the other logs if there are 5 rows
                if ( $totalStepCount > 4 ) {
                    // Add extra class for the show and hide of attachment
                    $logInfo = $logInfo . "<tr class='viewAllAttchLogsRow' style='display:none;'>";
                    $showViewAllLogs = true;
                }
                else {
                    $logInfo = $logInfo . "<tr>";
                }
                $totalStepCount++;
                
                
                $logInfo = $logInfo . "<td>".$row['stepNumber']."</td>";
                $logInfo = $logInfo . "<td>".$row['documentName']."</td>";
//                    echo "<td>".$row['activity']."</td>";

                // Add information icon, if any
                $logInfo = $logInfo . "<td>".$row['activity']." ";
                if ( $row['message'] != NULL ) {
                    $logInfo = $logInfo . "<img class='infoIcon' id='info_".$row['logId']."_".$scrnPostn."' src='images/log/info_blu.png' alt='Info Icon' width='12' height='12' /> ";
                }
                $logInfo = $logInfo . "</td>";

                // START - PRINT THE FULL NAME OF THE ACCOUNTED PERSON PROPERLY
                // If userId is NULL, print external account name instead
                $logInfo = $logInfo . "<td>";

                    if ( $row['exAccId'] == NULL ) {
                        if( $row['firstName'] != NULL ) {
                            $logInfo = $logInfo . $row['firstName']." ";
                        }

                        // Middle name is removed due to insufficient space
    //                        if( $row['middleName'] != NULL ) {
    //                            echo $row['middleName']." ";
    //                        }

                        $logInfo = $logInfo . $row['lastName']." ";
                    }
                    else {
                        $logInfo = $logInfo . $row['exAccName']." ";
                    }

                $logInfo = $logInfo . "</td>";
                // END - PRINT THE FULL NAME OF THE ACCOUNTED PERSON PROPERLY

                // START - DISPLAY ALL DEPARTMENTS AND POSITIONS OF THE USER
                $logInfo = displayDeptPos($row['userId'], $row['stepId'], 'MainDoc', $logInfo);
                // END - DISPLAY ALL DEPARTMENTS AND POSITIONS OF THE USER

                $logInfo = $logInfo . "<td>".$row['logDate']."</td>";
                $logInfo = $logInfo . "<td>".$row['logTime']."</td>";
                $logInfo = $logInfo . "</tr>";
            }
            
            // Add extra row for the view of all logs
            if ( 1 ) {
                $logInfo = $logInfo . "<tr><td colspan='9'align='center'>";
                $logInfo = $logInfo . "<a href='javascript:void(0)' id='viewAllAttchLogs'>View All</a>";
                $logInfo = $logInfo . "</td></tr>";
            }
        // END - ##############PRINT ATTACHMENT LOGS##############
    }
    
    $logInfo = $logInfo . "</table>";
    // THIS DIV WILL BE USED FOR SHOWING OF ACTIVITY INFORMATION DEPENDING ON THE HOVERED INFORMATION ICON
    $logInfo = $logInfo . "<div class='inviInfoIcn' id='inviInfoIcn_".$scrnPostn."' style='display:none;'></div>";
    
    $feed['logInfo'] = $logInfo;    
    echo json_encode($feed);
    
    // free result and close database
    mysql_free_result($result);
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
